//
//  ViewController2.m
//  201-数据存储
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController2.h"

@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITextField *IDtextField;
@property (nonatomic,strong) NSArray *stuArr;

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSArray *)stuArr{
    NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *tempArr = [myDefaults objectForKey:@"学生信息"];
    NSMutableArray *tempMutArr = [NSMutableArray array];
    if (tempArr == nil) {
        _stuArr = [NSArray array];
    }
    else{
        [tempMutArr addObjectsFromArray:tempArr];
        _stuArr = tempMutArr;
    }
    return _stuArr;
}

-(void)save{
   NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
    [myDefaults setObject:_stuArr forKey:@"学生信息"];
    [self.tableView reloadData];
    
}

- (IBAction)add:(id)sender {
    NSMutableDictionary *dic = [@{@"stuID":self.IDtextField.text,
                          @"stuName":self.nameTextField.text,
                          @"stuAge":self.ageTextField.text } mutableCopy];
    
    
    NSMutableArray *mutArr = [NSMutableArray array];
    [mutArr addObjectsFromArray:_stuArr];
    [mutArr addObject:dic];
    _stuArr = mutArr;
    [self save];
    
}

- (IBAction)delete:(id)sender {
    for (NSDictionary *dic in self.stuArr) {
        if ([dic[@"stuID"] isEqualToString:self.IDtextField.text]) {
            NSMutableArray *mutArr = [NSMutableArray array];
            [mutArr addObjectsFromArray:_stuArr];
            [mutArr removeObject:dic];
            _stuArr = mutArr;
            break;
        }
    }
    [self save];
}

- (IBAction)modify:(id)sender {
    
    for (int i = 0; i < _stuArr.count; i++) {
        NSMutableDictionary *dic = _stuArr[i];
        NSLog(@"%@",dic);
        if ([dic[@"stuID"] isEqualToString:self.IDtextField.text]) {
            NSMutableArray *temArr = [NSMutableArray array];
            [temArr addObjectsFromArray:_stuArr];
            [temArr removeObject:dic];
            NSMutableDictionary *dict = [dic mutableCopy];
            dict[@"stuName"] = self.nameTextField.text;
            NSLog(@"%@",dict[@"stuName"]);
            dict[@"stuAge"] = self.ageTextField.text;
            [temArr insertObject:dict atIndex:i];
            _stuArr = temArr;
            [self save];
        }

    }
}
- (IBAction)find:(id)sender {
    for (NSMutableDictionary *dic in self.stuArr) {
        if ([dic[@"stuID"] isEqualToString:self.IDtextField.text]) {
            self.nameTextField.text = dic[@"stuName"];
            self.ageTextField.text = dic[@"stuAge"];
        }
    }
}
- (IBAction)findAll:(id)sender {
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.stuArr.count;
    NSLog(@"%i",self.stuArr.count);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
    UILabel *labe1 = [cell.contentView viewWithTag:301];
    UILabel *labe2 = [cell.contentView viewWithTag:302];
    UILabel *labe3 = [cell.contentView viewWithTag:303];
    NSMutableDictionary *m = self.stuArr[indexPath.row];
    labe3.text = m[@"stuID"];
    labe2.text = m[@"stuName"];
    labe1.text = m[@"stuAge"];
    
    return cell;
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
